<?php
if (!defined('IN_IA')) {
    exit('Access Denied');
}
global $_W, $_GPC;

$commission_set = globalSetting('commission');
$op     = $operation = $_GPC['op'] ? $_GPC['op'] : 'display';
$groups = m('member')->getGroups();
$levels = m('member')->getLevels();
$shop   = globalSetting('shop');
if ($op == 'display') {
    $pindex    = max(1, intval($_GPC['page']));
    $psize     = 20;
    $condition = " and dm.uniacid=:uniacid";
    $params    = array(
        ':uniacid' => $_W['uniacid']
    );
    if (!empty($_GPC['realname'])) {
        $_GPC['realname'] = trim($_GPC['realname']);
        $condition .= ' and ( dm.realname like :realname or dm.nickname like :realname or dm.mobile like :realname or dm.openid=:ropenid)';
        $params[':realname'] = "%{$_GPC['realname']}%";
         $params[':ropenid'] = "{$_GPC['realname']}";
    }
    $condition .= ' and q.is_delete=0';
    if ($_GPC['isblack'] != '') {
        $condition .= ' and dm.isblack=' . intval($_GPC['isblack']);
    }
    if ($_GPC['reserve-area'] != '') {
        $condition .= ' and q.reserve_area="' . $_GPC['reserve-area'] . '"';
    }
    if (empty($starttime) || empty($endtime)) {
        $starttime = strtotime('-1 month');
        $endtime   = time();
    }
    
    if (empty($_GPC['order-type'])) {
        $_GPC['order-type'] = 'getto';
    }
    if (empty($_GPC['order-by'])) {
        $_GPC['order-by'] = 'desc';
    }
    $order_by = 'desc';
    if($_GPC['order-by'] == 'asc'){
        $order_by = 'asc';
    }
    if($_GPC['order-type'] == 'getto'){
        $order_type = 'getto_date';
        $order_str = 'q.getto_date '.$order_by;
    }
    else if($_GPC['order-type'] == 'setoff'){
        $order_type = 'setoff_date';
        $order_str = 'q.setoff_date '.$order_by;
    }
    if (!empty($_GPC['time'])) {
        $starttime = strtotime($_GPC['time']['start']);
        $endtime   = strtotime($_GPC['time']['end']);
        if ($_GPC['searchtime'] == '1') {
            $condition .= " AND q.".$order_type." >= :starttime AND q.".$order_type." <= :endtime ";
            $params[':starttime'] = $_GPC['time']['start'].' 00:00:00';
            $params[':endtime']   = $_GPC['time']['end'].' 23:59:59';
        }
    }

    $sql = "select dm.id,dm.openid,dm.avatar,q.realname,q.reserve_area,q.getto_flight,q.setoff_flight,q.getto_date,q.setoff_date,q.songji_time,q.hotel,q.people_num,q.packet_num,q.reserve_time,q.isread,q.id as reserve_id from " . tablename('eshop_member') . " dm " . " left join " . tablename('eshop_member_group') . " g on dm.groupid=g.id" . " left join " . tablename('eshop_member_reservation') . " q on q.uid=dm.id" . " where q.id is not null {$condition}  ORDER BY ".$order_str;
    if (empty($_GPC['export'])) {
        $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;
    }
    $list = pdo_fetchall($sql, $params);
    $number_count = 1;
    foreach ($list as &$row) {
        $row['count'] = $number_count;
        $number_count += 1;
    }
    unset($row);
    if ($_GPC['export'] == '1') {
        m('excel')->export($list, array(
            "title" => "会员预约-接机-" . date('Y-m-d-H-i', time()),
            "columns" => array(
                array(
                    'title' => '编号',
                    'field' => 'count',
                    'width' => 12
                ),
                array(
                    'title' => '姓名',
                    'field' => 'realname',
                    'width' => 12
                ),
                array(
                    'title' => '区域',
                    'field' => 'reserve_area',
                    'width' => 12
                ),
                array(
                    'title' => '接机时间',
                    'field' => 'getto_date',
                    'width' => 24
                ),
                array(
                    'title' => '航班号',
                    'field' => 'getto_flight',
                    'width' => 12
                ),
                array(
                    'title' => '酒店',
                    'field' => 'hotel',
                    'width' => 24
                ),
                array(
                    'title' => '人数',
                    'field' => 'people_num',
                    'width' => 12
                ),
                array(
                    'title' => '行李数',
                    'field' => 'packet_num',
                    'width' => 12
                )
            )
        ));
    }
    if ($_GPC['export'] == '2') {
        m('excel')->export($list, array(
            "title" => "会员预约-送机-" . date('Y-m-d-H-i', time()),
            "columns" => array(
                array(
                    'title' => '编号',
                    'field' => 'count',
                    'width' => 12
                ),
                array(
                    'title' => '姓名',
                    'field' => 'realname',
                    'width' => 12
                ),
                array(
                    'title' => '区域',
                    'field' => 'reserve_area',
                    'width' => 12
                ),
                array(
                    'title' => '送机时间',
                    'field' => 'songji_time',
                    'width' => 24
                ),
                array(
                    'title' => '航班号',
                    'field' => 'setoff_flight',
                    'width' => 12
                ),
                array(
                    'title' => '航班起飞时间',
                    'field' => 'setoff_date',
                    'width' => 24
                ),
                array(
                    'title' => '酒店',
                    'field' => 'hotel',
                    'width' => 24
                ),
                array(
                    'title' => '人数',
                    'field' => 'people_num',
                    'width' => 12
                ),
                array(
                    'title' => '行李数',
                    'field' => 'packet_num',
                    'width' => 12
                )
            )
        ));
    }
    $total           = pdo_fetchcolumn("select count(*) from" . tablename('eshop_member') . " dm " . " left join " . tablename('eshop_member_group') . " g on dm.groupid=g.id" . " left join " . tablename('eshop_member_reservation') . " q on q.uid=dm.id" . " where q.id is not null {$condition} ", $params);
    $pager           = pagination($total, $pindex, $psize);
} else if ($op == 'detail') {
    if (checksubmit('submit')){
        pdo_update('eshop_member_reservation', $_GPC['data'], array(
            'uid' => (int)$_GPC['id'],
            'id' => (int)$_GPC['reserve_id']
        ));
        message("修改成功！", $this->createWebUrl('member/reservation',array('op'=>'detail','id' => $_GPC['id'],'reserve_id' => $_GPC['reserve_id'])), 'success'); 
    }
    else if(checksubmit('mark')){
        pdo_update('eshop_member_reservation', array('isread' => 1), array(
            'uid' => (int)$_GPC['id'],
            'id' => (int)$_GPC['reserve_id']
        ));
        message("标记成功！", $this->createWebUrl('member/reservation',array('op'=>'detail','id' => $_GPC['id'],'reserve_id' => $_GPC['reserve_id'])), 'success'); 
    }
    else if(checksubmit('dismark')){
        pdo_update('eshop_member_reservation', array('isread' => 0), array(
            'uid' => (int)$_GPC['id'],
            'id' => (int)$_GPC['reserve_id']
        ));
        message("取消标记成功！", $this->createWebUrl('member/reservation',array('op'=>'detail','id' => $_GPC['id'],'reserve_id' => $_GPC['reserve_id'])), 'success'); 
    }
    else{

        $condition = " and id=" . intval($_GPC['id']);
        $sql = "select * from " . tablename('eshop_member') . " where 1 {$condition}";
        $member = pdo_fetchall($sql)[0];

        $pindex = max(1, intval($_GPC['page']));
        $psize = 20;
        $condition = " and id=" . intval($_GPC['reserve_id']);
        $sql = "select * from " . tablename('eshop_member_reservation') . " where 1 {$condition}";
        $sql .= " limit " . ($pindex - 1) * $psize . ',' . $psize;
        $row = pdo_fetchall($sql)[0];
        $total = pdo_fetchcolumn("select count(*) from " . tablename('eshop_member_reservation') . " where 1 {$condition}", $params);
        $pager = pagination($total, $pindex, $psize);
    }
} else if($op == 'delete') {
    if (empty($_GPC['reserve_id']) or $_GPC['reserve_id'] == '') {
        message("缺少预约id参数！");
    }
    pdo_update('eshop_member_reservation', array("is_delete" => 1), array("id" => $_GPC['reserve_id']));
    message("删除成功！", $this->createWebUrl('member/reservation',array('do'=>'member','m'=>'eshop')), 'success'); 
}

include $this->template('reservation');